package com.socool;


import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.socool.manager.MySocketIO;

public class Frag_YourDates extends ListFragment {

	View view;
	DateAdapter adapter;
	
	
	
	final String date[] ={"2013년 2월 28일 20시","2013년 3월 1일 19시","2013년 3월 3일 18시"};
	final String location[] ={"강남 와라와라","이태원 모우모우","홍대 나루수산"};
	final String with[] ={"박상권, 박상권2","이명박, 박근혜","노무현, 문재인"};
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	view = (LinearLayout) inflater.inflate(R.layout.frag_your_dates, container, false);
        return view;
    }
    
    PullToRefreshListView pullToRefreshView;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		pullToRefreshView = (PullToRefreshListView)view.findViewById(R.id.pull_to_refresh_listview);
		pullToRefreshView.setOnRefreshListener(new OnRefreshListener<ListView>() {
		    @Override
		    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		        // Do work to refresh the list here.
		        new GetDataTask().execute();
		    }
		});
		
		
		adapter = new DateAdapter(getActivity());
		for (int i = 0; i < date.length; i++) {
			//adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
			adapter.add(new DateItem(0,date[i],location[i],with[i]));
		}
		setListAdapter(adapter);
		
		
		
	}
	
	@Override
    public void onListItemClick(ListView parent, View v, int position, long id) { 
	
		Fragment newContent = null;
		String title="";
		switch(position){
		default:
			newContent = new Frag_Date_Detail();
			title = "Detail";
		
		}
		
		if (newContent != null)
			addFragment(newContent,title);
    }
	

	protected void addFragment(Fragment fragment,String title) {
 		if (getActivity() == null)
 			return;
 		
 		HomeActivity fca = (HomeActivity) getActivity();
 			fca.addFragment(fragment,title);
 		
 	}
	

	private class GetDataTask extends AsyncTask<Void, Void, DateItem[]> {
	  
	    @Override
	    protected void onPostExecute(DateItem[] result) {
	    	for(int i=0; i<result.length; i++){
	    		adapter.add(result[0]);	
	    	}
	    		    	
	    	// Call onRefreshComplete when the list has been refreshed.
	        pullToRefreshView.onRefreshComplete();
	        adapter.notifyDataSetChanged();

	        
	        
	        super.onPostExecute(result);
	    }

		@Override
		protected DateItem[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
			DateItem[] temp = {new DateItem()};
			return temp;
			
		}
	}
 
    
	private class DateItem {
		
		int imageRes;
		String date;
		String location;
		String member;
		DateItem(int imageRes, String date, String location, String member) {
			this.imageRes = imageRes; 
			this.date = date; 
			this.location = location; 
			this.member = member; 
		}
		DateItem(){
			this.imageRes = 0; 
			this.date = "2013년 언젠가"; 
			this.location = "한국 어딘가"; 
			this.member = "누군가와..";
		}
	}

	public class DateAdapter extends ArrayAdapter<DateItem> {

		public DateAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_dates, null);
			}
			
			if(getItem(position) !=null){
			ImageView icon = (ImageView) convertView.findViewById(R.id.IV_DateTeam);
			//icon.setImageResource(getItem(position).imageRes);
			TextView TV_date = (TextView) convertView.findViewById(R.id.TV_DateDate);
			TV_date.setText(getItem(position).date);
			
			TextView TV_location = (TextView) convertView.findViewById(R.id.TV_DateLocation);
			TV_location.setText(getItem(position).location);
			
			TextView TV_DateWith = (TextView) convertView.findViewById(R.id.TV_DateWith);
			TV_DateWith.setText(getItem(position).member);
			}
			
			

			
		
			
			return convertView;
			
			
		}

	}
}

